package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.TemplateInfoAddDTO;
import org.jsola.hr.dto.TemplateInfoUpdateDTO;
import org.jsola.hr.query.TemplateInfoQuery;
import org.jsola.hr.service.ITemplateInfoService;
import org.jsola.hr.vo.TemplateInfoListVO;
import org.jsola.hr.vo.TemplateInfoVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * @author zhr
 */
@RestController("hrTemplateInfoController")
@RequestMapping(HrConstants.API_PATH + "/v1/templateInfos")
public class TemplateInfoController implements ITemplateInfoControllerAPI {

    @Autowired
    private ITemplateInfoService templateInfoService;

    @Override
    @PostMapping
    public Result<TemplateInfoVO> save(@Valid @RequestBody TemplateInfoAddDTO templateInfoAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增模板信息权限校验

        return Result.success(
                templateInfoService.save(templateInfoAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody TemplateInfoUpdateDTO templateInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改模板信息权限校验

        int count = templateInfoService.update(templateInfoUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{templateInfoId}")
    public Result<Integer> delete(@PathVariable String templateInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除模板信息权限校验

        int count = templateInfoService.deleteByIds(tokenUser, templateInfoId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] templateInfoIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除模板信息权限校验

        int count = templateInfoService.deleteByIds(tokenUser, templateInfoIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{templateInfoId}")
    public Result<TemplateInfoVO> findById(@PathVariable String templateInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看模板信息权限校验

        return Result.success(
            templateInfoService.selectById(templateInfoId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<TemplateInfoListVO>> listAll(@Valid TemplateInfoQuery templateInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看模板信息权限校验

        return Result.success(
            templateInfoService.select(templateInfoQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<TemplateInfoListVO>> page(@Valid TemplateInfoQuery templateInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            templateInfoService.selectPage(templateInfoQuery, tokenUser.getSiteId())
        );
    }


}

